--- ========================================================= ---
---  Setup Revision, Version, Date and add to AceLibrary
--- ========================================================= ---
local VERSION = tonumber(("$Revision: 52619 $"):match("%d+"))

local DaHud = DaHud
DaHud_Module_Icons = DaHud:NewModule("Module_Icons", "AceEvent-2.0")
local self = DaHud_Icon
if DaHud.revision < VERSION then
	DaHud.version = "r" .. VERSION
	DaHud.revision = VERSION
	DaHud.date = ("$Date: 2007-10-19 21:07:04 -0400 (Fri, 19 Oct 2007) $"):match("%d%d%d%d%-%d%d%-%d%d")
end
DaHud.desc = "Icons Module"

--- ========================================================= ---
---  Setup Locals
--- ========================================================= ---
local L = DaHud:L("DaHud_Module_Icons")

--- ========================================================= ---
---  On Intitalize / Setup Database
--- ========================================================= ---
function DaHud_Module_Icons:OnInitialize()
	self.db = DaHud:AcquireDBNamespace("Module_Icons")
	DaHud:RegisterDefaults("Module_Icons", "profile", {
		numbers = {
			player = 6,
			target = 4,
			pet = 4,
			targettarget = 3,
			focus = 4,
		},
		player = {
			hudicon1 = {"DaHudPlayerStatusRestingIcon", "Resting Icon", true, "LOW", 8, 20, 20, 1, -185, -110, "player", "Interface\\CharacterFrame\\UI-StateIcon", 0.09, 0.43, 0.08, 0.42},
			hudicon2 = {"DaHudPlayerCombatIcon", "Combat Icon", true, "LOW", 8, 20, 20, 1, -185, -110, "player", "Interface\\CharacterFrame\\UI-StateIcon", 0.57, 0.90, 0.08, 0.41},
			hudicon3 = {"DaHudPlayerPvPIcon", "PvP Icon", true, "LOW", 8, 20, 20, 1, -185, -85, "player", "Interface\\TargetingFrame\\UI-PVP-FFA", 0.05, 0.605, 0.015, 0.57},
			hudicon4 = {"DaHudPlayerLeaderIcon", "Leader Icon", true, "LOW", 8, 15, 15, 1, -185, -60, "player", "Interface\\GroupFrame\\UI-Group-LeaderIcon", 0.1, 0.84, 0.14, 0.88},
			hudicon5 = {"DaHudPlayerMasterLooterIcon", "Master Looter Icon", true, "LOW", 8, 15, 15, 1, -185, -35, "player", "Interface\\GroupFrame\\UI-Group-MasterLooter", 0.15, 0.9, 0.15, 0.9},
			hudicon6 = {"DaHudPlayerRaidTargetIcon", "Raid Target Icon", true, "LOW", 8, 20, 20, 1, -150, -125, "player", "Interface\\TargetingFrame\\UI-RaidTargetingIcons", 0, 0, 0, 0},
		},
		target = {
			hudicon1 = {"DaHudTargetCombatIcon", "Combat Icon", true, "LOW", 8, 20, 20, 1, 185, -110, "target", "Interface\\CharacterFrame\\UI-StateIcon", 0.57, 0.90, 0.08, 0.41},
			hudicon2 = {"DaHudTargetPvPIcon", "PvP Icon", true, "LOW", 8, 20, 20, 1, 185, -85, "target", "Interface\\TargetingFrame\\UI-PVP-FFA", 0.05, 0.605, 0.015, 0.57},
			hudicon3 = {"DaHudTargetRaidTargetIcon", "Raid Target Icon", true, "LOW", 8, 20, 20, 1, 150, -125, "target", "Interface\\TargetingFrame\\UI-RaidTargetingIcons", 0, 0, 0, 0},
			hudicon4 = {"DaHudTargetEliteIcon", "Elite Icon", true, "LOW", 8, 64, 64, 1, 170, 120, "target", "Interface\\Addons\\DaHud\\Art\\Elite", 0, 0, 0, 0},
		},
		pet = {
			hudicon1 = {"DaHudPetCombatIcon", "Combat Icon", false, "LOW", 8, 20, 20, 1, -90, -110, "pet", "Interface\\CharacterFrame\\UI-StateIcon", 0.57, 0.90, 0.08, 0.41},
			hudicon2 = {"DaHudPetPvPIcon", "PvP Icon", false, "LOW", 8, 20, 20, 1, -90, -85, "pet", "Interface\\TargetingFrame\\UI-PVP-FFA", 0.05, 0.605, 0.015, 0.57},
			hudicon3 = {"DaHudPetRaidTargetIcon", "Raid Target Icon", true, "LOW", 8, 20, 20, 1, -115, -125, "pet", "Interface\\TargetingFrame\\UI-RaidTargetingIcons", 0, 0, 0, 0},
			hudicon4 = {"DaHudPetHappinessIcon", "Happiness Icon", true, "LOW", 8, 20, 20, 1, -115, 10, "pet", "Interface\\PetPaperDollFrame\\UI-PetHappiness", 0.1875, 0.375, 0, 0.359375},

		},
		targettarget = {
			hudicon1 = {"DaHudTargetTargetCombatIcon", "Combat Icon", false, "LOW", 8, 20, 20, 1, 90, -110, "targettarget", "Interface\\CharacterFrame\\UI-StateIcon", 0.57, 0.90, 0.08, 0.41},
			hudicon2 = {"DaHudTargetTargetPvPIcon", "PvP Icon", false, "LOW", 8, 20, 20, 1, 90, -85, "targettarget", "Interface\\TargetingFrame\\UI-PVP-FFA", 0.05, 0.605, 0.015, 0.57},
			hudicon3 = {"DaHudTargetTargetRaidTargetIcon", "Raid Target Icon", true, "LOW", 8, 20, 20, 1, 115, -125, "targettarget", "Interface\\TargetingFrame\\UI-RaidTargetingIcons", 0, 0, 0, 0},
		},
		focus = {
			hudicon1 = {"DaHudFocusCombatIcon", "Combat Icon", false, "LOW", 8, 20, 20, 1, 235, -110, "focus", "Interface\\CharacterFrame\\UI-StateIcon", 0.57, 0.90, 0.08, 0.41},
			hudicon2 = {"DaHudFocusPvPIcon", "PvP Icon", false, "LOW", 8, 20, 20, 1, 235, -85, "focus", "Interface\\TargetingFrame\\UI-PVP-FFA", 0.05, 0.605, 0.015, 0.57},
			hudicon3 = {"DaHudFocusRaidTargetIcon", "Raid Target Icon", true, "LOW", 8, 20, 20, 1, 210, -125, "focus", "Interface\\TargetingFrame\\UI-RaidTargetingIcons", 0, 0, 0, 0},
			hudicon4 = {"DaHudFocusEliteIcon", "Elite Icon", true, "LOW", 8, 64, 64, 1, 220, 120, "focus", "Interface\\Addons\\DaHud\\Art\\Elite", 0, 0, 0, 0},
		},
	})
end

--- ========================================================= ---
---  On Enable
--- ========================================================= ---
function DaHud_Module_Icons:OnEnable()
	self:SetupOptions()
	self:CreateFrames()
	self:UpdateAllFrames()
	
	self:RegisterEvent("DaHud_SetConfigMode")
	self:RegisterEvent("DaHud_ReloadAllFrames")

	self:ScheduleRepeatingEvent("DaHud_Icon_ProcessAllFrames", self.ProcessAllFrames, 0.05, self)
end

--- ========================================================= ---
---  On Disable
--- ========================================================= ---
function DaHud_Module_Icons:OnDisable()
	self:UnregisterAllEvents();
end

--- ========================================================= ---
---  DaHud_SetConfigMode
--- ========================================================= ---
function DaHud_Module_Icons:DaHud_SetConfigMode()
	self:UpdateAllFrames()
end

--- ========================================================= ---
---  DaHud_ReloadAllFrames
--- ========================================================= ---
function DaHud_Module_Icons:DaHud_ReloadAllFrames()
	self:SetupOptions()
	self:UpdateAllFrames()
end

--- ========================================================= ---
---  Setup Validates for Options
--- ========================================================= ---
local strataValidate = {
	BACKGROUND = L["Background"],
	LOW = L["Low"],
	MEDIUM = L["Medium"],
	HIGH = L["High"],
	DIALOG = L["Dialog"],
	TOOLTIP = L["Tooltip"],
}

local strataValidateDesc = {}

--- ========================================================= ---
---  Setup Options
--- ========================================================= ---
function DaHud_Module_Icons:SetupOptions()
	DaHud.options.args.iconframes = {
		name = L["Icon Frames"],
		desc = L["Change the Icon Frames options."],
		type = 'group',
		args = {
		},
	}

	DaHud.options.args.iconframes.args.playericons = {
		name = L["Player Icons"],
		desc = L["Change the Player Icons options."],
		type = 'group',
		args = {
		},
	}

	for i=1, self.db.profile.numbers.player do
		local frame = "hudicon"..i
		local valunit = "player"
		
		self:UpdateMainOption(frame, valunit)
		self:UpdateShowOption(frame, valunit)
		self:UpdateFramestrataOption(frame, valunit)
		self:UpdateFramelevelOption(frame, valunit)
		self:UpdateWidthOption(frame, valunit)
		self:UpdateHeightOption(frame, valunit)
		self:UpdateScaleOption(frame, valunit)
		self:UpdatePosXOption(frame, valunit)
		self:UpdatePosYOption(frame, valunit)
	end
	
	DaHud.options.args.iconframes.args.targeticons = {
		name = L["Target Icons"],
		desc = L["Change the Target Icons options."],
		type = 'group',
		args = {
		},
	}

	for i=1, self.db.profile.numbers.target do
		local frame = "hudicon"..i
		local valunit = "target"
		
		self:UpdateMainOption(frame, valunit)
		self:UpdateShowOption(frame, valunit)
		self:UpdateFramestrataOption(frame, valunit)
		self:UpdateFramelevelOption(frame, valunit)
		self:UpdateWidthOption(frame, valunit)
		self:UpdateHeightOption(frame, valunit)
		self:UpdateScaleOption(frame, valunit)
		self:UpdatePosXOption(frame, valunit)
		self:UpdatePosYOption(frame, valunit)
	end
	
	DaHud.options.args.iconframes.args.peticons = {
		name = L["Pet Icons"],
		desc = L["Change the Pet Icons options."],
		type = 'group',
		args = {
		},
	}

	for i=1, self.db.profile.numbers.pet do
		local frame = "hudicon"..i
		local valunit = "pet"
		
		self:UpdateMainOption(frame, valunit)
		self:UpdateShowOption(frame, valunit)
		self:UpdateFramestrataOption(frame, valunit)
		self:UpdateFramelevelOption(frame, valunit)
		self:UpdateWidthOption(frame, valunit)
		self:UpdateHeightOption(frame, valunit)
		self:UpdateScaleOption(frame, valunit)
		self:UpdatePosXOption(frame, valunit)
		self:UpdatePosYOption(frame, valunit)
	end
	
	DaHud.options.args.iconframes.args.targettargeticons = {
		name = L["Target of Target Icons"],
		desc = L["Change the Target of Target Icons options."],
		type = 'group',
		args = {
		},
	}

	for i=1, self.db.profile.numbers.targettarget do
		local frame = "hudicon"..i
		local valunit = "targettarget"
		
		self:UpdateMainOption(frame, valunit)
		self:UpdateShowOption(frame, valunit)
		self:UpdateFramestrataOption(frame, valunit)
		self:UpdateFramelevelOption(frame, valunit)
		self:UpdateWidthOption(frame, valunit)
		self:UpdateHeightOption(frame, valunit)
		self:UpdateScaleOption(frame, valunit)
		self:UpdatePosXOption(frame, valunit)
		self:UpdatePosYOption(frame, valunit)
	end
	
	DaHud.options.args.iconframes.args.focusicons = {
		name = L["Focus Icons"],
		desc = L["Change the Focus Icons options."],
		type = 'group',
		args = {
		},
	}

	for i=1, self.db.profile.numbers.focus do
		local frame = "hudicon"..i
		local valunit = "focus"
		
		self:UpdateMainOption(frame, valunit)
		self:UpdateShowOption(frame, valunit)
		self:UpdateFramestrataOption(frame, valunit)
		self:UpdateFramelevelOption(frame, valunit)
		self:UpdateWidthOption(frame, valunit)
		self:UpdateHeightOption(frame, valunit)
		self:UpdateScaleOption(frame, valunit)
		self:UpdatePosXOption(frame, valunit)
		self:UpdatePosYOption(frame, valunit)
	end
end

--- ========================================================= ---
---  Update Main Option
--- ========================================================= ---
function DaHud_Module_Icons:UpdateMainOption(frame, valunit)
	local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])
	
	DaHud.options.args.iconframes.args[valunit.."icons"].args[frame] = {
		name = name,
		desc = L["Change this Icons options."],
		type = 'group',
		args = {
		}
	}
end

--- ========================================================= ---
---  Update Show Option
--- ========================================================= ---
function DaHud_Module_Icons:UpdateShowOption(frame, valunit)
	DaHud.options.args.iconframes.args[valunit.."icons"].args[frame].args.show = {
		name = L["Show"],
		desc = L["Show this Frame."],
		type = 'toggle',
		get = function()
			local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])
			return show
		end,
		set = function()
			local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])
			self:SaveOptions(frame, "show", not(show), valunit)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Framestrata Option
--- ========================================================= ---
function DaHud_Module_Icons:UpdateFramestrataOption(frame, valunit)
	DaHud.options.args.iconframes.args[valunit.."icons"].args[frame].args.framestrata = {
		name = L["Frame Strata"],
		desc = L["Choose the layer the portrait is drawn on."],
		type = 'text',
		validate = strataValidate,
		validateDesc = strataValidateDesc,
		get = function()
			local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])
			return framestrata
		end,
		set = function(value)
			local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])
			self:SaveOptions(frame, "framestrata", value, valunit)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Framelevel Option
--- ========================================================= ---
function DaHud_Module_Icons:UpdateFramelevelOption(frame, valunit)
	DaHud.options.args.iconframes.args[valunit.."icons"].args[frame].args.framelevel = {
		name = L["Frame Level"],
		desc = L["Change the Frame Level it is drawn on."],
		type = 'range',
		min = 1,
		max = 12,
		step = 1,
		bigStep = 2,
		get = function()
			local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])
			return framelevel
		end,
		set = function(value) 
			self:SaveOptions(frame, "framelevel", value, valunit)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Width Option
--- ========================================================= ---
function DaHud_Module_Icons:UpdateWidthOption(frame, valunit)
	DaHud.options.args.iconframes.args[valunit.."icons"].args[frame].args.width = {
		name = L["Width"],
		desc = L["Change the Width."],
		type = 'range',
		min = 0,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])
			return width
		end,
		set = function(value) 
			self:SaveOptions(frame, "width", value, valunit)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Height Option
--- ========================================================= ---
function DaHud_Module_Icons:UpdateHeightOption(frame, valunit)
	DaHud.options.args.iconframes.args[valunit.."icons"].args[frame].args.height = {
		name = L["Height"],
		desc = L["Change the Height."],
		type = 'range',
		min = 0,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])
			return height
		end,
		set = function(value) 
			self:SaveOptions(frame, "height", value, valunit)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update Scale Option
--- ========================================================= ---
function DaHud_Module_Icons:UpdateScaleOption(frame, valunit)
	DaHud.options.args.iconframes.args[valunit.."icons"].args[frame].args.scale = {
		name = L["Scale"],
		desc = L["Change the Scale."],
		type = 'range',
		min = 0.25,
		max = 3,
		step = 0.01,
		bigStep = 0.05,
		get = function()
			local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])
			return scale
		end,
		set = function(value) 
			self:SaveOptions(frame, "scale", value, valunit)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update POS X Option
--- ========================================================= ---
function DaHud_Module_Icons:UpdatePosXOption(frame, valunit)
	DaHud.options.args.iconframes.args[valunit.."icons"].args[frame].args.posx = {
		name = L["Pos X"],
		desc = L["Change the X Position."],
		type = 'range',
		min = -500,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])
			return posx
		end,
		set = function(value) 
			self:SaveOptions(frame, "posx", value, valunit)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Update POS Y Option
--- ========================================================= ---
function DaHud_Module_Icons:UpdatePosYOption(frame, valunit)
	DaHud.options.args.iconframes.args[valunit.."icons"].args[frame].args.posy = {
		name = L["Pos Y"],
		desc = L["Change the Y Position."],
		type = 'range',
		min = -500,
		max = 500,
		step = 1,
		bigStep = 20,
		get = function()
			local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])
			return posy
		end,
		set = function(value) 
			self:SaveOptions(frame, "posy", value, valunit)
			self:UpdateAllFrames()
		end,
	}
end

--- ========================================================= ---
---  Save Options
--- ========================================================= ---
function DaHud_Module_Icons:SaveOptions(frame, var, value, valunit)
	local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][frame])	

	if var == "show" then
		show = value
	elseif var == "framestrata" then
		framestrata = value
	elseif var == "framelevel" then
		framelevel = value
	elseif var == "width" then
		width = value
	elseif var == "height" then
		height = value
	elseif var == "scale" then
		scale = value
	elseif var == "posx" then
		posx = value
	elseif var == "posy" then
		posy = value
	end
	
	self.db.profile[valunit][frame] = {framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom}
end	

--- ========================================================= ---
---  Create Frame
--- ========================================================= ---
function DaHud_Module_Icons:CreateFrame(frametype, valunit, vals)
	local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(self.db.profile[valunit][vals])
	local f = CreateFrame(frametype, framename, getglobal("DaHudFrame"))

	f:SetWidth(width)
	f:SetHeight(height)
	f:SetScale(scale)
	f:SetPoint("CENTER", getglobal("DaHudFrame"), "CENTER", posx, posy)
	f:SetFrameStrata(framestrata)
	f:SetFrameLevel(framelevel)

	local t = f:CreateTexture(framename.."Texture", "ARTWORK")
	t:SetTexture(tex)
	if left ~= 0 then
		t:SetTexCoord(left, right, top, bottom)
	end
	t:SetAllPoints(framename)
	
	f:Show()

	return f
end

--- ========================================================= ---
---  Create Frames
--- ========================================================= ---
function DaHud_Module_Icons:CreateFrames()
	for i=1, self.db.profile.numbers.player do
		self:CreateFrame("Frame", "player", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.target do
		self:CreateFrame("Frame", "target", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.pet do
		self:CreateFrame("Frame", "pet", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.targettarget do
		self:CreateFrame("Frame", "targettarget", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.focus do
		self:CreateFrame("Frame", "focus", "hudicon"..i)
	end
end

--- ========================================================= ---
---  Update All Frames
--- ========================================================= ---
function DaHud_Module_Icons:UpdateAllFrames()
	for i=1, self.db.profile.numbers.player do
		self:UpdateFrames("player", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.target do
		self:UpdateFrames("target", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.pet do
		self:UpdateFrames("pet", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.targettarget do
		self:UpdateFrames("targettarget", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.focus do
		self:UpdateFrames("focus", "hudicon"..i)
	end
end

--- ========================================================= ---
---  Update Frames
--- ========================================================= ---
function DaHud_Module_Icons:UpdateFrames(valunit, vals)
	local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(DaHud.db.namespaces.Module_Icons.profile[valunit][vals])
	local f = getglobal(framename)
	
	f:SetWidth(width)
	f:SetHeight(height)
	f:SetScale(scale)
	f:SetPoint("CENTER", getglobal("DaHudFrame"), "CENTER", posx, posy)
	f:SetFrameStrata(framestrata)
	f:SetFrameLevel(framelevel)
end

--- ========================================================= ---
---  Process All Frames
--- ========================================================= ---
function DaHud_Module_Icons:ProcessAllFrames()
	for i=1, self.db.profile.numbers.player do
		self:ProcessFrames("player", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.target do
		self:ProcessFrames("target", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.pet do
		self:ProcessFrames("pet", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.targettarget do
		self:ProcessFrames("targettarget", "hudicon"..i)
	end
	for i=1, self.db.profile.numbers.focus do
		self:ProcessFrames("focus", "hudicon"..i)
	end
end

--- ========================================================= ---
---  Process Frames
--- ========================================================= ---
function DaHud_Module_Icons:ProcessFrames(valunit, vals)
	local framename, name, show, framestrata, framelevel, width, height, scale, posx, posy, unit, tex, left, right, top, bottom = unpack(DaHud.db.namespaces.Module_Icons.profile[valunit][vals])
	local has
	local f = getglobal(framename)
	local tex = getglobal(framename.."Texture")
	
	if not(show) then
		f:Hide()
		return
	else
		f:Show()
	end
	
	if DaHud_ConfigMode then
		f:Show()
		return
	end
	
	if framename == "DaHudPlayerStatusRestingIcon" then
		if not(IsResting()) then
			f:Hide()
			return
		end
	end
	if framename == "DaHudPlayerCombatIcon" then
		if not(UnitAffectingCombat(unit)) then
			f:Hide()
			return
		end
	end
	if framename == "DaHudPlayerPvPIcon" then
		if UnitIsPVPFreeForAll(unit) then
			has = 'FFA'
		else
			local faction = UnitFactionGroup(unit)
			if faction and (UnitIsPVP(unit)) then
				has = faction
			end
		end
		if has then
			tex:SetTexture("Interface\\TargetingFrame\\UI-PVP-" .. has)
			if has == "Horde" then
				tex:SetTexCoord(0.08, 0.58, 0.045, 0.545)
			elseif has == "Alliance" then
				tex:SetTexCoord(0.07, 0.58, 0.06, 0.57)
			else
				tex:SetTexCoord(0.05, 0.605, 0.015, 0.57)
			end
		else
			f:Hide()
			return
		end
	end
	if framename == "DaHudPlayerLeaderIcon" then
		if not(IsPartyLeader()) and not(IsRaidLeader()) and not(IsRaidOfficer()) then
			f:Hide()
			return
		end
	end
	if framename == "DaHudPlayerMasterLooterIcon" then
		local _, lootmaster = GetLootMethod()
		if not(lootmaster == 0) then
			f:Hide()
			return
		end
	end
	if framename == "DaHudPlayerRaidTargetIcon" then
		local icon = GetRaidTargetIndex(unit);
		if not(icon) then
			f:Hide()
			return
		else
			SetRaidTargetIconTexture(tex, icon)
		end
	end

	if framename == "DaHudTargetCombatIcon" then
		if not(UnitAffectingCombat(unit)) then
			f:Hide()
			return
		end
	end
	if framename == "DaHudTargetPvPIcon" then
		if UnitIsPVPFreeForAll(unit) then
			has = 'FFA'
		else
			local faction = UnitFactionGroup(unit)
			if faction and (UnitIsPVP(unit)) then
				has = faction
			end
		end
		if has then
			tex:SetTexture("Interface\\TargetingFrame\\UI-PVP-" .. has)
			if has == "Horde" then
				tex:SetTexCoord(0.08, 0.58, 0.045, 0.545)
			elseif has == "Alliance" then
				tex:SetTexCoord(0.07, 0.58, 0.06, 0.57)
			else
				tex:SetTexCoord(0.05, 0.605, 0.015, 0.57)
			end
		else
			f:Hide()
			return
		end
	end
	if framename == "DaHudTargetRaidTargetIcon" then
		local icon = GetRaidTargetIndex(unit);
		if not(icon) then
			f:Hide()
			return
		else
			SetRaidTargetIconTexture(tex, icon)
		end
	end
	if framename == "DaHudTargetEliteIcon" then
		local classification = UnitClassification(unit)
		if not(classification) or classification == "normal" then
			f:Hide()
			return
		else
			if classification == "worldboss" or classification == "elite" then
				tex:SetTexture("Interface\\Addons\\DaHud\\Art\\Elite")
			elseif classification == "rare" or classification == "rareelite" then
				tex:SetTexture("Interface\\Addons\\DaHud\\Art\\Rare")
			end
		end
	end
	
	if framename == "DaHudPetCombatIcon" then
		if not(UnitAffectingCombat(unit)) then
			f:Hide()
			return
		end
	end
	if framename == "DaHudPetPvPIcon" then
		if UnitIsPVPFreeForAll(unit) then
			has = 'FFA'
		else
			local faction = UnitFactionGroup(unit)
			if faction and (UnitIsPVP(unit)) then
				has = faction
			end
		end
		if has then
			tex:SetTexture("Interface\\TargetingFrame\\UI-PVP-" .. has)
			if has == "Horde" then
				tex:SetTexCoord(0.08, 0.58, 0.045, 0.545)
			elseif has == "Alliance" then
				tex:SetTexCoord(0.07, 0.58, 0.06, 0.57)
			else
				tex:SetTexCoord(0.05, 0.605, 0.015, 0.57)
			end
		else
			f:Hide()
			return
		end
	end
	if framename == "DaHudPetRaidTargetIcon" then
		local icon = GetRaidTargetIndex(unit);
		if not(icon) then
			f:Hide()
			return
		else
			SetRaidTargetIconTexture(tex, icon)
		end
	end
	if framename == "DaHudPetHappinessIcon" then
		local happiness, damagePercentage, loyaltyRate = GetPetHappiness()
		if not(happiness) then
			f:Hide()
			return
		else
			if happiness == 3 then
				tex:SetTexCoord(0, 0.1875, 0, 0.359375)
			elseif happiness == 2 then
				tex:SetTexCoord(0.1875, 0.375, 0, 0.359375)
			elseif happiness == 1 then
				tex:SetTexCoord(0.375, 0.5625, 0, 0.359375)
			else
				f:Hide()
			end
		end
	end
	
	if framename == "DaHudTargetTargetCombatIcon" then
		if not(UnitAffectingCombat(unit)) then
			f:Hide()
			return
		end
	end
	if framename == "DaHudTargetTargetPvPIcon" then
		if UnitIsPVPFreeForAll(unit) then
			has = 'FFA'
		else
			local faction = UnitFactionGroup(unit)
			if faction and (UnitIsPVP(unit)) then
				has = faction
			end
		end
		if has then
			tex:SetTexture("Interface\\TargetingFrame\\UI-PVP-" .. has)
			if has == "Horde" then
				tex:SetTexCoord(0.08, 0.58, 0.045, 0.545)
			elseif has == "Alliance" then
				tex:SetTexCoord(0.07, 0.58, 0.06, 0.57)
			else
				tex:SetTexCoord(0.05, 0.605, 0.015, 0.57)
			end
		else
			f:Hide()
			return
		end
	end
	if framename == "DaHudTargetTargetRaidTargetIcon" then
		local icon = GetRaidTargetIndex(unit);
		if not(icon) then
			f:Hide()
			return
		else
			SetRaidTargetIconTexture(tex, icon)
		end
	end

	
	if framename == "DaHudFocusCombatIcon" then
		if not(UnitAffectingCombat(unit)) then
			f:Hide()
			return
		end
	end
	if framename == "DaHudFocusPvPIcon" then
		if UnitIsPVPFreeForAll(unit) then
			has = 'FFA'
		else
			local faction = UnitFactionGroup(unit)
			if faction and (UnitIsPVP(unit)) then
				has = faction
			end
		end
		if has then
			tex:SetTexture("Interface\\TargetingFrame\\UI-PVP-" .. has)
			if has == "Horde" then
				tex:SetTexCoord(0.08, 0.58, 0.045, 0.545)
			elseif has == "Alliance" then
				tex:SetTexCoord(0.07, 0.58, 0.06, 0.57)
			else
				tex:SetTexCoord(0.05, 0.605, 0.015, 0.57)
			end
		else
			f:Hide()
			return
		end
	end
	if framename == "DaHudFocusRaidTargetIcon" then
		local icon = GetRaidTargetIndex(unit);
		if not(icon) then
			f:Hide()
			return
		else
			SetRaidTargetIconTexture(tex, icon)
		end
	end
	if framename == "DaHudFocusEliteIcon" then
		local classification = UnitClassification(unit)
		if not(classification) or classification == "normal" then
			f:Hide()
			return
		else
			if classification == "worldboss" or classification == "elite" then
				tex:SetTexture("Interface\\Addons\\DaHud\\Art\\Elite")
			elseif classification == "rare" or classification == "rareelite" then
				tex:SetTexture("Interface\\Addons\\DaHud\\Art\\Rare")
			end
		end
	end
end